﻿using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using X.WPF.Controls.Models;

namespace X.WPF.Controls.UserControls
{
    /// <summary>
    /// 单一堆叠图表
    /// </summary>
    public partial class SingleStackedChart : UserControl
    {
        #region # 字段及构造器

        /// <summary>
        /// 静态构造器
        /// </summary>
        static SingleStackedChart()
        {
            //注册依赖属性
            SingleStackedChart.StackedRatiosProperty = DependencyProperty.Register(nameof(SingleStackedChart.StackedRatios), typeof(ObservableCollection<StackedRatio>), typeof(SingleStackedChart), new PropertyMetadata(new ObservableCollection<StackedRatio>(), SingleStackedChart.StackedRatiosChangedCallback));

            //注册路由事件
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public SingleStackedChart()
        {
            this.InitializeComponent();
        }

        #endregion

        #region # 依赖属性

        #region 堆积占比列表 —— ObservableCollection<StackedRatio> StackedRatios
        /// <summary>
        /// 堆积占比列表依赖属性
        /// </summary>
        public static DependencyProperty StackedRatiosProperty;

        /// <summary>
        /// 堆积占比列表
        /// </summary>
        public ObservableCollection<StackedRatio> StackedRatios
        {
            get => (ObservableCollection<StackedRatio>)base.GetValue(SingleStackedChart.StackedRatiosProperty);
            set => base.SetValue(SingleStackedChart.StackedRatiosProperty, value);
        }
        #endregion

        #endregion

        #region # 回调方法

        #region 堆积占比列表改变回调方法 —— static void StackedRatiosChangedCallback(...
        /// <summary>
        /// 堆积占比列表改变回调方法
        /// </summary>
        private static void StackedRatiosChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
        {
            SingleStackedChart singleStackedChart = (SingleStackedChart)dependencyObject;

            if (singleStackedChart.StackedRatios == null)
                return;

            singleStackedChart.StackedRatios.CollectionChanged += singleStackedChart.SingleStackedChartsItemsChanged;
            singleStackedChart.Rerender(singleStackedChart.StackedRatios);
        }

        #endregion

        #region 堆积占比列表元素改变回调方法 —— void SingleStackedChartsItemsChanged(object sender...
        /// <summary>
        /// 堆积占比列表元素改变回调方法
        /// </summary>
        private void SingleStackedChartsItemsChanged(object sender, NotifyCollectionChangedEventArgs eventArgs)
        {
            ObservableCollection<StackedRatio> shapes = (ObservableCollection<StackedRatio>)sender;
            this.Rerender(shapes);
        }
        #endregion

        #endregion

        #region # 方法

        // Private 

        #region 重新渲染 —— void Rerender(ObservableCollection<StackedRatio> stackedRatios)
        /// <summary>
        /// 重新渲染
        /// </summary>
        private void Rerender(ObservableCollection<StackedRatio> stackedRatios)
        {
            this.MainContainer.Children.Clear();
            this.MainContainer.ColumnDefinitions.Clear();

            if (stackedRatios == null)
                return;

            int index = 0;
            foreach (StackedRatio stackedRatio in stackedRatios)
            {
                ColumnDefinition columnDefinition = new ColumnDefinition
                {
                    Width = new GridLength(stackedRatio.Ratio, GridUnitType.Star)
                };
                this.MainContainer.ColumnDefinitions.Add(columnDefinition);
                StackPanel stackPanel = new StackPanel
                {
                    Background = new SolidColorBrush(stackedRatio.Color),
                };
                stackPanel.SetValue(Grid.ColumnProperty, index);
                this.MainContainer.Children.Add(stackPanel);
                index++;
            }
        }
        #endregion

        #endregion

    }
}